package com.ptadmin.report.mws.sample;

import java.util.Arrays;

import com.amazonaws.mws.MarketplaceWebService;
import com.amazonaws.mws.MarketplaceWebServiceClient;
import com.amazonaws.mws.MarketplaceWebServiceConfig;
import com.amazonaws.mws.MarketplaceWebServiceException;
import com.amazonaws.mws.model.GetReportListRequest;
import com.amazonaws.mws.model.GetReportListResponse;
import com.amazonaws.mws.model.GetReportListResult;
import com.amazonaws.mws.model.IdList;
import com.amazonaws.mws.model.ReportInfo;
import com.amazonaws.mws.model.ResponseMetadata;
import com.amazonaws.mws.model.TypeList;

public class GetReportList {

	public static void main(String[] args) {
		/************************************************************************
		 * Access Key ID and Secret Access Key ID, obtained from: http://aws.amazon.com
		 ***********************************************************************/
		final String accessKeyId = "AKIAI2JEZAYVZZZ3GHGA";
		final String secretAccessKey = "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";

		final String appName = "ptadmin";
		final String appVersion = "1.0.0";

		MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();

		config.setServiceURL("https://mws.amazonservices.com/");

		MarketplaceWebService service = new MarketplaceWebServiceClient(accessKeyId, secretAccessKey, appName,
				appVersion, config);

		final String merchantId = "A2N95VZZXMVRLW";
		final String sellerDevAuthToken = "amzn.mws.52b2d181-1d65-65a8-04f7-e12ce26cfad5";

		final IdList reportRequestIds = new IdList(Arrays.asList(
        		"50007017859"));
		
		final TypeList reportTypes = new TypeList(Arrays.asList(
				"_GET_FLAT_FILE_OPEN_LISTINGS_DATA_"));
		
		GetReportListRequest request = new GetReportListRequest();
		request.setMerchant(merchantId);
		request.setMWSAuthToken(sellerDevAuthToken);
		request.setReportRequestIdList(reportRequestIds);
		request.setReportTypeList(reportTypes);

        // @TODO: set request parameters here

        invokeGetReportList(service, request);
	}

	/**
	 * Get Report List request sample returns a list of reports; by default the most
	 * recent ten reports, regardless of their acknowledgement status
	 * 
	 * @param service
	 *            instance of MarketplaceWebService service
	 * @param request
	 *            Action to invoke
	 */
	public static void invokeGetReportList(MarketplaceWebService service, GetReportListRequest request) {
		try {

			GetReportListResponse response = service.getReportList(request);

			System.out.println("GetReportList Action Response");
			System.out.println("=============================================================================");
			System.out.println();

			System.out.print("    GetReportListResponse");
			System.out.println();
			if (response.isSetGetReportListResult()) {
				System.out.print("        GetReportListResult");
				System.out.println();
				GetReportListResult getReportListResult = response.getGetReportListResult();
				if (getReportListResult.isSetNextToken()) {
					System.out.print("            NextToken");
					System.out.println();
					System.out.print("                " + getReportListResult.getNextToken());
					System.out.println();
				}
				if (getReportListResult.isSetHasNext()) {
					System.out.print("            HasNext");
					System.out.println();
					System.out.print("                " + getReportListResult.isHasNext());
					System.out.println();
				}
				java.util.List<ReportInfo> reportInfoListList = getReportListResult.getReportInfoList();
				for (ReportInfo reportInfoList : reportInfoListList) {
					System.out.print("            ReportInfoList");
					System.out.println();
					if (reportInfoList.isSetReportId()) {
						System.out.print("                ReportId");
						System.out.println();
						System.out.print("                    " + reportInfoList.getReportId());
						System.out.println();
					}
					if (reportInfoList.isSetReportType()) {
						System.out.print("                ReportType");
						System.out.println();
						System.out.print("                    " + reportInfoList.getReportType());
						System.out.println();
					}
					if (reportInfoList.isSetReportRequestId()) {
						System.out.print("                ReportRequestId");
						System.out.println();
						System.out.print("                    " + reportInfoList.getReportRequestId());
						System.out.println();
					}
					if (reportInfoList.isSetAvailableDate()) {
						System.out.print("                AvailableDate");
						System.out.println();
						System.out.print("                    " + reportInfoList.getAvailableDate());
						System.out.println();
					}
					if (reportInfoList.isSetAcknowledged()) {
						System.out.print("                Acknowledged");
						System.out.println();
						System.out.print("                    " + reportInfoList.isAcknowledged());
						System.out.println();
					}
					if (reportInfoList.isSetAcknowledgedDate()) {
						System.out.print("                AcknowledgedDate");
						System.out.println();
						System.out.print("                    " + reportInfoList.getAcknowledgedDate());
						System.out.println();
					}
				}
			}
			if (response.isSetResponseMetadata()) {
				System.out.print("        ResponseMetadata");
				System.out.println();
				ResponseMetadata responseMetadata = response.getResponseMetadata();
				if (responseMetadata.isSetRequestId()) {
					System.out.print("            RequestId");
					System.out.println();
					System.out.print("                " + responseMetadata.getRequestId());
					System.out.println();
				}
			}
			System.out.println();
			System.out.println(response.getResponseHeaderMetadata());
			System.out.println();

		} catch (MarketplaceWebServiceException ex) {
			System.out.println("Caught Exception: " + ex.getMessage());
			System.out.println("Response Status Code: " + ex.getStatusCode());
			System.out.println("Error Code: " + ex.getErrorCode());
			System.out.println("Error Type: " + ex.getErrorType());
			System.out.println("Request ID: " + ex.getRequestId());
			System.out.print("XML: " + ex.getXML());
			System.out.println("ResponseHeaderMetadata: " + ex.getResponseHeaderMetadata());
		}
	}

}
